package com.chuanyang.driverh.mainhome.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.mainhome.DriverJoinActivity;
import com.chuanyang.driverh.mainhome.FleetJoinActivity;
import com.chuanyang.driverh.mainhome.MotorcadeMsgActivity;
import com.chuanyang.driverh.mainhome.SearchJiaMengActivity;
import com.chuanyang.driverh.mainhome.adapter.RecommendAdapter;
import com.chuanyang.driverh.response2.join.ResultAddList;
import com.chuanyang.driverh.responseresult.Result;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.LogUtils;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.StringUtil;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;
import com.chuanyang.driverh.utils.widget.CustomMeasureHeightListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.nereo.multi_image_selector.bean.Image;
import okhttp3.Call;

public class DriverNotJoinFragment extends Fragment implements View.OnClickListener{

    private ImageView iv_close_ad;
    private RelativeLayout rl_ad;
    private ImageView iv_to_join;
    private CustomMeasureHeightListView lv_tuijian;

    private RecommendAdapter recommendAdapter;
    private int page = 1;
    private List<ResultAddList.DataBeanX.DataBean> dataBeanList = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_driver_not_join, container, false);
        initView(view);

        // Inflate the layout for this fragment
        return view;
    }

    private void initView(View view){

        iv_close_ad = (ImageView) view.findViewById(R.id.iv_close_ad);
        iv_close_ad.setOnClickListener(this);
        rl_ad = (RelativeLayout) view.findViewById(R.id.rl_ad);
        iv_to_join = (ImageView) view.findViewById(R.id.iv_to_join);
        iv_to_join.setOnClickListener(this);
        lv_tuijian = (CustomMeasureHeightListView) view.findViewById(R.id.lv_tuijian);
        recommendAdapter = new RecommendAdapter(getActivity());
        lv_tuijian.setAdapter(recommendAdapter);
        getRecommendData(page);


        lv_tuijian.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (visibleItemCount + firstVisibleItem == totalItemCount) {
                    View lastVisibleItemView = lv_tuijian.getChildAt(totalItemCount - firstVisibleItem - 1);
                    if (lastVisibleItemView != null && lastVisibleItemView.getBottom() == view.getHeight()) {
                        // 滑动到了底部
                        page = page + 1;
                        getRecommendData(page);
                    } else {
                    }
                } else {
                }
            }
        });

        lv_tuijian.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), MotorcadeMsgActivity.class);
                intent.putExtra("data", "" + dataBeanList.get(position).getFleetId());
                startActivity(intent);
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_close_ad:
                rl_ad.setVisibility(View.GONE);
                break;
            case R.id.iv_to_join:
                //跳转到加盟的界面
//                Intent intent = new Intent(getActivity(),DriverJoinActivity.class);
//                startActivity(intent);
                Intent intent = new Intent(getActivity(),SearchJiaMengActivity.class);
                startActivity(intent);
                break;
        }

    }

    //获取推荐的列表

    private void getRecommendData(int page) {
        DialogUtil.showLoadingDialog(getActivity(),"加载中...");
        Map<String,Object> params=new HashMap<String, Object>();
        params.put("length", "10");
        params.put("driverId", SharedPrefUtil.getUserId());
        params.put("page", page);
        MyHttpUtil.selectRecommendBusiness(getActivity(),params, new MyCallback<ResultAddList>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
                //springView.onFinishFreshAndLoad();
            }
            @Override
            public void onSuccess(ResultAddList result, int id) {

                if (result.getCode() == ConstUtil.OK ) {
                    try {
                        dataBeanList.addAll(result.getData().getData());
                        recommendAdapter.setNewData(dataBeanList);
                    } catch (Exception e) {
                    }
                }else{
                    if (!StringUtil.isBlank(result.getMsg()))
                        PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });
    }

}
